package cn.wj.school.app.manage;


import cn.wj.school.app.cache.ClientCacheMap;
import cn.wj.school.app.dto.LoginRespMsg;
import cn.wj.school.app.ui.view.StageView;
import cn.wj.school.cloud.dto.SyUserDTO;
import cn.wj.school.cloud.enums.UserType;
import cn.wj.school.common.Constants;
import cn.wj.school.common.controller.StageController;
import cn.wj.school.common.dto.BaseMsg;
import cn.wj.school.common.enums.MsgType;
import cn.wj.school.common.enums.UiFactory;
import cn.wj.school.common.manager.BaseManager;
import com.alibaba.fastjson.JSONObject;
import io.netty.channel.Channel;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.apache.commons.lang.StringUtils;

/**
 * @Author: yueding
 * @Description:
 * @Date: Created in 11:13 2018/12/29
 * @Modified By: yueding Copyright(c) cai-inc.com
 */
public class LoginManager implements BaseManager {


    /**
     * beanName
     *
     * @return
     */
    @Override
    public String beanName() {
        return MsgType.LOGIN.getBeanName();
    }

    /**
     * 处理Msg
     *
     * @param msg
     */
    @Override
    public void handleMsg(BaseMsg msg, Channel channel) {
        LoginRespMsg loginRespMsg = (LoginRespMsg) msg;
        if (loginRespMsg.getSuccess() == Constants.TRUE) {
            SyUserDTO syUserDTO = JSONObject.parseObject(loginRespMsg.getUserInfo(),SyUserDTO.class);
            if (UserType.STUDENT.name().equals(syUserDTO.getType())
                    && StringUtils.isNotBlank(loginRespMsg.getServerIp())) {
               //启动学生ui
            } else if (UserType.TEACHER.name().equals(syUserDTO.getType())) {
                //教师端启动教师端ui

            }
            UiFactory.INSTANCE.runTaskInFxThread(() -> {
                StageController stageController = UiFactory.INSTANCE.getStageController();
                Stage mainStage = stageController.loadStage(StageView.id.MAIN_VIEW, StageView.layout.MAIN_VIEW,
                        StageStyle.DECORATED);
                mainStage.setOnCloseRequest((e)->e.consume());
                mainStage.setTitle("main");
                mainStage.setIconified(true);//最小化
                Label username = (Label) mainStage.getScene().getRoot().lookup("#userName");
                username.setText(syUserDTO.getUserName() + "  欢迎你!");
                stageController.switchStage(StageView.id.MAIN_VIEW, StageView.id.LOGIN_VIEW);
            });
            ClientCacheMap.putCurrentUser(syUserDTO);
        } else {
            UiFactory.INSTANCE.runTaskInFxThread(() -> {
                StageController stageController = UiFactory.INSTANCE.getStageController();
                Stage stage = stageController.getStageBy(StageView.id.LOGIN_VIEW);
                Pane errPane = (Pane) stage.getScene().getRoot().lookup("#errorPane");
                errPane.setVisible(true);
                Label errTips = (Label) stage.getScene().getRoot().lookup("#errorTips");
                errTips.setText(loginRespMsg.getError());
            });
        }
    }

    /**
     * 获取消息对象
     */
    @Override
    public BaseMsg getObject() {
        return new LoginRespMsg();
    }
}
